/**
 * @filename:MaterialServiceImpl Dec 8, 2023
 * @project Wonder Vue Admin Api  1.1.0
 * Copyright(c) 2022 Wonder Co. Ltd. 
 * All right reserved. 
 */
package com.wonder.app.service.impl;

import com.vtuzx.core.bean.dto.VtuzxMap;
import com.wonder.app.domain.Material;
import com.wonder.app.domain.Synbd;
import com.wonder.app.mapper.MaterialDao;
import com.wonder.app.mapper.SynbdDao;
import com.wonder.app.service.MaterialService;
import com.wonder.app.request.MaterialRequest;
import com.wonder.common.common.CommonPage;
import com.wonder.common.utils.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**   
 * @Description:TODO(物料管理服务实现)
 *
 * @version: 1.1.0
 * @author: Wonder
 * 
 */
@Service
public class MaterialServiceImpl  extends ServiceImpl<MaterialDao, Material> implements MaterialService  {

	@Autowired
	private MaterialDao materialDao;

	@Autowired
	private SynbdDao synbdDao;

    /**
     * 验证物料管理信息
     * 
     * @param material
     * @param type
     * @return String
     */
    @Override
    public String validate(Material material,String type){
    
    
       return "";
    }
    
    
    /**
	 * Description:物料管理分页返回
	 * @param request //检索条件
	 * @param page //分页参数
	 * @return IPage<Material>   //分页记录
	 */
	@Override 
	public IPage<Material> selectList(MaterialRequest request, Page<Material> page){
	    LambdaQueryWrapper<Material> lambdaQueryWrapper = new LambdaQueryWrapper<>();
	    
	    if (null != request.getFNUMBER()) {
	       lambdaQueryWrapper.like(Material::getFNUMBER, request.getFNUMBER());
	      }               

	    if(null != request.getFname()){
	    	lambdaQueryWrapper.like(Material::getFName,request.getFname());
		}
        if (StringUtils.hasText(request.getFNUMBER())) {
			    lambdaQueryWrapper.like(Material::getFNUMBER, request.getFNUMBER()); 
		    } 
             
        
		lambdaQueryWrapper.orderByDesc(Material::getId);	
		Page<Material> selectPage = this.baseMapper.selectPage(page, lambdaQueryWrapper);
		return selectPage;
    }

	@Override
	public VtuzxMap getMaterialByKey( String key) {
		QueryWrapper<Material> wrapper = new QueryWrapper<>();
		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
		Matcher m = p.matcher(key);
		if (!m.find()){//key不是汉字按照编码查询
			wrapper.like("FNUMBER",key);
		} else {//按照物料名称查询
			wrapper.like("FName",key);
		}

		Page<Material> page = new Page<>(0,20);
		Page<Material> pageList = materialDao.selectPage(page, wrapper);

		return new VtuzxMap("data", CommonPage.restPage(pageList));
	}

	@Override
	public Synbd getReloadResult(String type) {
		LambdaQueryWrapper<Synbd> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(Synbd::getFtype,type);
		wrapper.orderByDesc(Synbd::getReloadEndTime);
		wrapper.last("limit 1");
		Synbd synbd = synbdDao.selectOne(wrapper);
		return synbd;
	}


}